<template>
	<view class="">
		<view class="fill">
			<view class="userItem">
				姓名：
				<input placeholder="请输入姓名" type="text" v-model="name" maxlength="15" />
			</view>
			<view class="userItem">
				证件类型：
				<picker style="width: 60%;" :range="array" :value="index" @change="selectType">
					<view>{{ array[index] }}</view>
				</picker>
			</view>
			<view class="userItem">
				证件号：
				<text v-if="check">{{ cardId }}</text>
				<input v-if="!check" placeholder="请输入证件号" type="text" v-model="cardId" />
			</view>
			<view class="userItem">
				手机号：
				<input type="text" placeholder="请输入手机号" v-model="phone" />
			</view>
		</view>
		<button class="btn" type="default" @click="setUser">添加</button>
	</view>
</template>

<script>
import utils from '@/static/common/utils.js';
import value from '@/static/value/value.js';
import common from '@/static/common/check.js';
export default {
	data() {
		return {
			name: '',
			cardId: '',
			phone: '',
			check: false,
			array: ['身份证', '回乡证', '护照', '台胞证'],
			index: 0
		};
	},
	onLoad(options) {
		if (options.userinfo) {
			var t = value.value.alertUserInfo;
			this.name = t.name;
			this.cardId = t.cardId;
			this.phone = t.phone;
			this.check = true;
			this.id = t.id;
		}
	},

	methods: {
		selectType(e) {
			if (this.check) {
				return;
			}
			this.index = e.detail.value;
		},
		save(o) {
			const that = this;
			this.$http
				.request({
					url: this.check ? `/Member/UpdateTakeManInfo` : `/Member/AddTakeManInfo`,
					method: 'post',
					data: o
				})
				.then(res => {
					if (!res.data.result) {
						that.$ut.Msg(res.data.msg);
						return;
					}
					uni.navigateBack({});
				})
				.catch(err => {
					console.log(err);
				});
		},
		setUser() {
			const that = this;
			if (this.check) {
				var objUp = {
					name: this.name,
					phone: this.phone,
					takemanId: this.id,
					ticks: utils.utils.GetTicks(),
					channel: 'Xcx'
				};
				objUp.sign = utils.utils.GetSign(objUp);
				this.save(objUp);
				return;
			}

			if (this.name !== '' && this.cardId !== '' && this.phone !== '') {
				// 去空格
				var phone = this.phone.replace(/\s/g, '');
				var name = this.name.replace(/\s/g, '');
				var cardId = this.cardId.replace(/\s/g, '');
			} else {
				this.$ut.Msg('请确认信息都已填写');
				return;
			}
			if (!common.ChkUtil.isMobile(phone)) {
				this.$ut.Msg('请核实手机号是否正确');
				return;
			}
			let hui = /^([A-Z]\d{6,11}(\(\w{1}\))?)$/;
			let tai = /^\d{8}|^\d{18}|^[a-zA-Z0-9]{10}$/;
			let hu = /^([a-zA-z]|[0-9]){5,17}$/;
			var obj = {
				name: name,
				cardId: cardId,
				phone: phone,
				ticks: utils.utils.GetTicks(),
				channel: 'Xcx',
				idType: Number(this.index) + 1
			};
			obj.sign = utils.utils.GetSign(obj);
			// 身份证
			if (this.index == 0) {
				if (common.ChkUtil.isIdCardNew(cardId)) {
					this.save(obj);
					return;
				} else {
					this.$ut.Msg('请核对证件号是否正确');
				}
				return;
			}
			// 回乡证判断
			if (this.index == 1) {
				if (hui.test(cardId)) {
					this.save(obj);
					return;
				} else {
					this.$ut.Msg('请核对证件号是否正确');
				}
				return;
			}
			// 护照判断
			if (this.index == 2) {
				if (hu.test(cardId)) {
					this.save(obj);
					return;
				} else {
					this.$ut.Msg('请核对证件号是否正确');
				}
				return;
			}
			// 台胞证判断
			if (this.index == 3) {
				if (tai.test(cardId)) {
					this.save(obj);
					return;
				} else {
					this.$ut.Msg('请核对证件号是否正确');
				}
				returnK;
			}
		}
	}
};
</script>

<style scoped>
.fill view {
	height: 100upx;
	line-height: 100upx;
	display: flex;
	border-bottom: 1px solid #cccccc;
	padding-left: 30upx;
}

.fill view input {
	height: 100upx;
	line-height: 100upx;
}

.btn {
	width: 92%;
	top: 70upx;
	margin: auto;
	color: #ffffff;
	font-weight: bold;
	background-color: #01a1e9;
}
</style>
